import { isString, isEmpty } from "@pureadmin/utils";
import { useMultiTagsStoreHook } from "@/store/modules/multiTags";
import {
  useRouter,
  useRoute,
  type LocationQueryRaw,
  type RouteParamsRaw
} from "vue-router";

export function useDetail() {
  const route = useRoute();
  const router = useRouter();
  const getParameter = isEmpty(route.params) ? route.query : route.params;

  function toDetail(
    parameter: LocationQueryRaw | RouteParamsRaw,
    model:
      | "addReconcoilation"
      | "editReconcoilation"
      | "viewReconcoilation"
      | "examineReconcoilation"
      | "addSettlement"
      | "editSettlement"
      | "viewSettlement"
      | "examineSettlement"
      | "addSupReconcoilation"
      | "editSupReconcoilation"
      | "viewSupReconcoilation"
      | "examineSupReconcoilation"
      | "addSupSettlement"
      | "editSupSettlement"
      | "viewSupSettlement"
      | "examineSupSettlement"
      | "viewElevatorReconcoilation" // 查看电梯项目对账
      | "editElevatorReconcoilation" // 编辑电梯项目对账
      | "examineElevatorReconcoilation" // 电梯项目对账：核对 对账确认 云彩确认
      | "viewElevatorSettlement" // 查看电梯项目结算
      | "editElevatorSettlement" // 编辑电梯项目结算
      | "examineElevatorSettlement" // 电梯项目结算：核对 对账确认 云彩确认
      | "viewElevatorSupReconcoilation" // 查看电梯供应商对账
      | "editElevatorSupReconcoilation" // 编辑电梯供应商对账
      | "examineElevatorSupReconcoilation" // 电梯供应商对账：核对 对账确认 云彩确认
      | "viewElevatorSupSettlement" // 查看电梯供应商结算
      | "editElevatorSupSettlement" // 编辑电梯供应商结算
      | "examineElevatorSupSettlement" // 电梯供应商结算：核对 对账确认 云彩确认
  ) {
    Object.keys(parameter).forEach(param => {
      if (!isString(parameter[param])) {
        parameter[param] = parameter[param].toString();
      }
    });
    /////////////////// 项目结算相关 ///////////////////
    // 新建项目对账
    if (model === "addReconcoilation") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_settlement/project/components/projectReconciliationDetail",
        name: "projectReconciliationDetail",
        query: parameter,
        meta: {
          title: `【新建】电线电缆对账单`
        }
      });
      router.push({ name: "projectReconciliationDetail", query: parameter });
    }
    // 编辑项目对账
    else if (model === "editReconcoilation") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_settlement/project/components/projectReconciliationDetail",
        name: "projectReconciliationDetail",
        query: parameter,
        meta: {
          title: `【编辑】${parameter.title}对账单`
        }
      });
      router.push({ name: "projectReconciliationDetail", query: parameter });
    }
    // 查看项目对账
    else if (model === "viewReconcoilation") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_settlement/project/components/projectReconciliationDetail",
        name: "projectReconciliationDetail",
        query: parameter,
        meta: {
          title: `【查看】${parameter.title}对账单`
        }
      });
      router.push({ name: "projectReconciliationDetail", query: parameter });
    }
    // 核对/确认项目对账
    else if (model === "examineReconcoilation") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_settlement/project/components/projectReconciliationDetail",
        name: "projectReconciliationDetail",
        query: parameter,
        meta: {
          title: `【确认】${parameter.title}对账单`
        }
      });
      router.push({ name: "projectReconciliationDetail", query: parameter });
    }
    // 查看电梯项目对账
    else if (model === "viewElevatorReconcoilation") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_settlement/elevator/components/projectReconciliationDetail",
        name: "elevatorReconciliationDetail",
        query: parameter,
        meta: {
          title: `【查看】${parameter.title}对账单`
        }
      });
      router.push({ name: "elevatorReconciliationDetail", query: parameter });
    }
    // 编辑电梯项目对账
    else if (model === "editElevatorReconcoilation") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_settlement/elevator/components/projectReconciliationDetail",
        name: "elevatorReconciliationDetail",
        query: parameter,
        meta: {
          title: `【编辑】${parameter.title}对账单`
        }
      });
      router.push({ name: "elevatorReconciliationDetail", query: parameter });
    }
    // 核对/确认电梯项目对账
    else if (model === "examineElevatorReconcoilation") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_settlement/elevator/components/projectReconciliationDetail",
        name: "elevatorReconciliationDetail",
        query: parameter,
        meta: {
          title: `【确认】${parameter.title}对账单`
        }
      });
      router.push({ name: "elevatorReconciliationDetail", query: parameter });
    }
    // 新建项目结算
    else if (model === "addSettlement") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_settlement/project/components/projectSettlementDetail",
        name: "projectSettlementDetail",
        query: parameter,
        meta: {
          title: `【新建】电线电缆结算单`
        }
      });
      router.push({ name: "projectSettlementDetail", query: parameter });
    }
    // 编辑项目结算
    else if (model === "editSettlement") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_settlement/project/components/projectSettlementDetail",
        name: "projectSettlementDetail",
        query: parameter,
        meta: {
          title: `【编辑】${parameter.title}结算单`
        }
      });
      router.push({ name: "projectSettlementDetail", query: parameter });
    }
    // 查看项目结算
    else if (model === "viewSettlement") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_settlement/project/components/projectSettlementDetail",
        name: "projectSettlementDetail",
        query: parameter,
        meta: {
          title: `【查看】${parameter.title}结算单`
        }
      });
      router.push({ name: "projectSettlementDetail", query: parameter });
    }
    // 核对/确认项目结算
    else if (model === "examineSettlement") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_settlement/project/components/projectSettlementDetail",
        name: "projectSettlementDetail",
        query: parameter,
        meta: {
          title: `【确认】${parameter.title}结算单`
        }
      });
      router.push({ name: "projectSettlementDetail", query: parameter });
    }
    // 查看电梯项目结算
    else if (model === "viewElevatorSettlement") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_settlement/elevator/components/projectSettlementDetail",
        name: "elevatorSettlementDetail",
        query: parameter,
        meta: {
          title: `【查看】${parameter.title}结算单`
        }
      });
      router.push({ name: "elevatorSettlementDetail", query: parameter });
    }
    // 编辑电梯项目结算
    else if (model === "editElevatorSettlement") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_settlement/elevator/components/projectSettlementDetail",
        name: "elevatorSettlementDetail",
        query: parameter,
        meta: {
          title: `【编辑】${parameter.title}结算单`
        }
      });
      router.push({ name: "elevatorSettlementDetail", query: parameter });
    }
    // 核对/确认电梯项目结算
    else if (model === "examineElevatorSettlement") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_settlement/elevator/components/projectSettlementDetail",
        name: "elevatorSettlementDetail",
        query: parameter,
        meta: {
          title: `【确认】${parameter.title}结算单`
        }
      });
      router.push({ name: "elevatorSettlementDetail", query: parameter });
    }
    /////////////////// 供应商结算相关 ///////////////////
    // 新建供应商对账
    else if (model === "addSupReconcoilation") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_settlement/supplier/components/supplierReconciliationDetail",
        name: "supplierReconciliationDetail",
        query: parameter,
        meta: {
          title: `【新建】电线电缆供应商对账单`
        }
      });
      router.push({ name: "supplierReconciliationDetail", query: parameter });
    }
    // 编辑供应商对账
    else if (model === "editSupReconcoilation") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_settlement/supplier/components/supplierReconciliationDetail",
        name: "supplierReconciliationDetail",
        query: parameter,
        meta: {
          title: `【编辑】${parameter.title}对账单`
        }
      });
      router.push({ name: "supplierReconciliationDetail", query: parameter });
    }
    // 查看供应商对账
    else if (model === "viewSupReconcoilation") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_settlement/supplier/components/supplierReconciliationDetail",
        name: "supplierReconciliationDetail",
        query: parameter,
        meta: {
          title: `【查看】${parameter.title}对账单`
        }
      });
      router.push({ name: "supplierReconciliationDetail", query: parameter });
    }
    // 核对/确认供应商对账
    else if (model === "examineSupReconcoilation") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_settlement/supplier/components/supplierReconciliationDetail",
        name: "supplierReconciliationDetail",
        query: parameter,
        meta: {
          title: `【确认】${parameter.title}对账单`
        }
      });
      router.push({ name: "supplierReconciliationDetail", query: parameter });
    }
    // 查看电梯供应商对账
    else if (model === "viewElevatorSupReconcoilation") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_settlement/elevatorSupplier/components/supplierReconciliationDetail",
        name: "elevatorSupplierReconciliationDetail",
        query: parameter,
        meta: {
          title: `【查看】${parameter.title}对账单`
        }
      });
      router.push({
        name: "elevatorSupplierReconciliationDetail",
        query: parameter
      });
    }
    // 编辑电梯供应商对账
    else if (model === "editElevatorSupReconcoilation") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_settlement/elevatorSupplier/components/supplierReconciliationDetail",
        name: "elevatorSupplierReconciliationDetail",
        query: parameter,
        meta: {
          title: `【编辑】${parameter.title}对账单`
        }
      });
      router.push({
        name: "elevatorSupplierReconciliationDetail",
        query: parameter
      });
    }
    // 核对/确认电梯供应商对账
    else if (model === "examineElevatorSupReconcoilation") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_settlement/elevatorSupplier/components/supplierReconciliationDetail",
        name: "elevatorSupplierReconciliationDetail",
        query: parameter,
        meta: {
          title: `【确认】${parameter.title}对账单`
        }
      });
      router.push({
        name: "elevatorSupplierReconciliationDetail",
        query: parameter
      });
    }
    // 新建供应商结算
    else if (model === "addSupSettlement") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_settlement/supplier/components/supplierSettlementDetail",
        name: "supplierSettlementDetail",
        query: parameter,
        meta: {
          title: `【新建】电线电缆供应商结算单`
        }
      });
      router.push({ name: "supplierSettlementDetail", query: parameter });
    }
    // 编辑供应商结算
    else if (model === "editSupSettlement") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_settlement/supplier/components/supplierSettlementDetail",
        name: "supplierSettlementDetail",
        query: parameter,
        meta: {
          title: `【编辑】${parameter.title}结算单`
        }
      });
      router.push({ name: "supplierSettlementDetail", query: parameter });
    }
    // 查看供应商结算
    else if (model === "viewSupSettlement") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_settlement/supplier/components/supplierSettlementDetail",
        name: "supplierSettlementDetail",
        query: parameter,
        meta: {
          title: `【查看】${parameter.title}结算单`
        }
      });
      router.push({ name: "supplierSettlementDetail", query: parameter });
    }
    // 核对/确认供应商结算
    else if (model === "examineSupSettlement") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_settlement/supplier/components/supplierSettlementDetail",
        name: "supplierSettlementDetail",
        query: parameter,
        meta: {
          title: `【确认】${parameter.title}结算单`
        }
      });
      router.push({ name: "supplierSettlementDetail", query: parameter });
    }
    // 查看电梯供应商结算
    else if (model === "viewElevatorSupSettlement") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_settlement/elevatorSupplier/components/supplierSettlementDetail",
        name: "elevatorSupplierSettlementDetail",
        query: parameter,
        meta: {
          title: `【查看】${parameter.title}结算单`
        }
      });
      router.push({
        name: "elevatorSupplierSettlementDetail",
        query: parameter
      });
    }
    // 编辑电梯供应商结算
    else if (model === "editElevatorSupSettlement") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_settlement/elevatorSupplier/components/supplierSettlementDetail",
        name: "elevatorSupplierSettlementDetail",
        query: parameter,
        meta: {
          title: `【编辑】${parameter.title}结算单`
        }
      });
      router.push({
        name: "elevatorSupplierSettlementDetail",
        query: parameter
      });
    }
    // 核对/确认电梯供应商结算
    else if (model === "examineElevatorSupSettlement") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_settlement/elevatorSupplier/components/supplierSettlementDetail",
        name: "elevatorSupplierSettlementDetail",
        query: parameter,
        meta: {
          title: `【确认】${parameter.title}结算单`
        }
      });
      router.push({
        name: "elevatorSupplierSettlementDetail",
        query: parameter
      });
    }
  }

  // 用于页面刷新，重新获取浏览器地址栏参数并保存到标签页
  const initToDetail = (
    model:
      | "addReconcoilation"
      | "editReconcoilation"
      | "viewReconcoilation"
      | "examineReconcoilation"
      | "addSettlement"
      | "editSettlement"
      | "viewSettlement"
      | "examineSettlement"
      | "addSupReconcoilation"
      | "editSupReconcoilation"
      | "viewSupReconcoilation"
      | "examineSupReconcoilation"
      | "addSupSettlement"
      | "editSupSettlement"
      | "viewSupSettlement"
      | "examineSupSettlement"
      | "viewElevatorReconcoilation" // 查看电梯项目对账
      | "editElevatorReconcoilation" // 编辑电梯项目对账
      | "examineElevatorReconcoilation" // 电梯项目对账：核对 对账确认 云彩确认
      | "viewElevatorSettlement" // 查看电梯项目结算
      | "editElevatorSettlement" // 编辑电梯项目结算
      | "examineElevatorSettlement" // 电梯项目结算：核对 对账确认 云彩确认
      | "viewElevatorSupReconcoilation" // 查看电梯供应商对账
      | "editElevatorSupReconcoilation" // 编辑电梯供应商对账
      | "examineElevatorSupReconcoilation" // 电梯供应商对账：核对 对账确认 云彩确认
      | "viewElevatorSupSettlement" // 查看电梯供应商结算
      | "editElevatorSupSettlement" // 编辑电梯供应商结算
      | "examineElevatorSupSettlement" // 电梯供应商结算：核对 对账确认 云彩确认
  ) => {
    if (getParameter) toDetail(getParameter, model);
  };

  return { toDetail, initToDetail, getParameter, router };
}
